package EPrints::Plugin::Screen::Shelves::ShelvesOverwrite;
use EPrints;
use strict;

our @ISA = ("EPrints::Plugin");

package EPrints::Plugin::Screen::Shelf::Remove;

sub action_cancel
{
        my( $self ) = @_;

        $self->{processor}->{screenid} = "Shelf::View";
}

package EPrints::Plugin::Screen::Shelf::EditMetadata;

sub action_save
{
        my( $self ) = @_;

        $self->workflow->update_from_form( $self->{processor} );
        my $shelfid = $self->{processor}->{shelfid};
        $self->uncache_workflow;

        $self->{processor}->{shelfid} = $shelfid;

        $self->{processor}->{screenid} = "Shelf::View";


}

package EPrints::Plugin::Screen::Shelf::EditItems;

sub render
{
        my( $self ) = @_;

        my $session = $self->{session};
        my $shelf = $self->{processor}->{shelf};

        my $chunk = $session->make_doc_fragment;

        if( $session->get_lang->has_phrase( $self->html_phrase_id( "intro" ) ) )
        {
                my $intro_div_outer = $session->make_element( "div", class => "ep_toolbox" );
                my $intro_div = $session->make_element( "div", class => "ep_toolbox_content" );
                $intro_div->appendChild( $self->html_phrase( "intro" ) );
                $intro_div_outer->appendChild( $intro_div );
                $chunk->appendChild( $intro_div_outer );
        }

        my $imagesurl = $session->get_repository->get_conf( "rel_path" )."/style/images";

        my %options;
        $options{session} = $session;
        $options{id} = "ep_review_instructions";
        $options{title} = $session->html_phrase( "Plugin/Screen/Shelf/EditItems:help_title" );
        $options{content} = $session->html_phrase( "Plugin/Screen/Shelf/EditItemsItems:help" );
        $options{collapsed} = 1;
        $options{show_icon_url} = "$imagesurl/help.gif";
        my $box = $session->make_element( "div", style=>"text-align: left" );
        $box->appendChild( EPrints::Box::render( %options ) );
        $chunk->appendChild( $box );

	my $link_back = $session->make_element("a", class=>"back_to_shelf", href=>"/cgi/users/home?screen=Shelf::View&shelfid=".$shelf->get_id);
	$link_back->appendChild($self->html_phrase("back_to_shelf"));
	$chunk->appendChild($link_back);

        ### Get the items owned by the current user
        my $ds = $session->get_repository->get_dataset( "eprint" );

        my $list = EPrints::List->new(
                session => $session,
                dataset => $ds,
                ids => $shelf->get_value('items'),
        );

        if ($list->count < 1)
        {
                $chunk->appendChild($session->render_message('warning', $self->html_phrase('shelf_empty')));
                return $chunk;
        }

        my $controls = $session->make_element('table');
        my $tr = $session->make_element('tr');
        $controls->appendChild($tr);

        my $td = $session->make_element('td');
        $tr->appendChild($td);
        $td->appendChild(
                $session->render_button(
                        class=>"ep_form_action_button",
                        name=>"_action_null",
                        value => $session->phrase( 'Plugin/Screen/Shelf/RemoveSelectedItems:title' ) ) );

        $td =  $session->make_element('td');
        $tr->appendChild($td);

        $td->appendChild( $session->render_button(
                class=>"ep_form_action_button",
                name=>"_action_reorder",
                value => $self->phrase( "reorder" )
        ) );

        my $eprint_ds = $session->get_repository->get_dataset('eprint');
        my ( $sortids, $sort_labels );
        foreach my $fieldid (@{$session->get_repository->get_conf('shelf_order_fields')})
        {
                push @{$sortids}, $fieldid;
                push @{$sortids}, '-' . $fieldid;

                my $fieldname = EPrints::Utils::tree_to_utf8($eprint_ds->get_field($fieldid)->render_name($session));

                $sort_labels->{$fieldid} = $fieldname;
                $sort_labels->{'-'.$fieldid} = "$fieldname (desc)";

        }

        $td->appendChild( $session->render_option_list(
                name => 'order',
                height => 1,
                multiple => 0,
                'values' => $sortids,
                labels => $sort_labels ) );


        my %hidden_items = map { $_ => 1 } @{$shelf->get_hidden_items() || []};

        # Paginate list
        my %opts = (
                params => {
                        screen => "Shelf::EditItems",
                        shelfid => $self->{processor}->{shelfid},
                },
                below_results => $controls,
                render_result => sub {
                        my( $session, $e ) = @_;

                        my( $tr, $td );

                        $tr = $session->make_element( "tr" );

                        $tr->setAttribute( 'class', 'ep_shelves_item_hidden' ) if( $hidden_items{$e->get_id} );

                        $td = $session->make_element( 'td', class=>"ep_columns_cell ep_columns_cell_first" );
                        $td->appendChild(
                                $session->render_noenter_input_field(
                                type => "checkbox",
                                name => "eprintids",
                                value => $e->get_id )
                        );

                        $tr->appendChild($td); #table cell for tickbox

                        $td = $session->make_element( "td", class=>"ep_columns_cell ep_columns_cell_$_"  );
                        $tr->appendChild( $td );
                        $td->appendChild( $e->render_citation_link );
                        $self->{processor}->{eprint} = $e;
                        $self->{processor}->{eprintid} = $e->get_id;
                        $td = $session->make_element( "td", class=>"ep_columns_cell ep_columns_cell_last", align=>"left" );
                        $tr->appendChild( $td );
                        $td->appendChild(
                                $self->render_action_list_icons( "shelf_items_eprint_actions", ['shelfid','eprintid','_buffer_order','_buffer_offset'] ) );
                        delete $self->{processor}->{eprint};

                        return $tr;
                },
        );


        # Add form
        my $div = $session->make_element( "div", class=>"ep_shelf_actions" );
        my $form = $session->render_form( "post" );
        $form->appendChild( $session->render_hidden_field( "screen", "Shelf::EditItemsActions" ) );
        $form->appendChild($self->render_hidden_bits);

        my $table = $session->make_element('table');

        $table->appendChild(EPrints::Paginate->paginate_list( $session, "_buffer", $list, %opts ));
        $form->appendChild($table);

        $chunk->appendChild($form);

        return $chunk;
}


